package net.hn.hnms.biz.power.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import net.hn.hnms.biz.api.feign.model.DataStateVo;
import net.hn.hnms.biz.common.jpa.utils.ResponsePage;
import net.hn.hnms.biz.power.domain.DeviceInfo;
import net.hn.hnms.biz.power.domain.HisSensorMonitor;
import net.hn.hnms.biz.power.domain.NowSensorMonitor;
import net.hn.hnms.biz.power.domain.PointHistoryListVo;
import net.hn.hnms.biz.power.domain.dto.PointDto;
import net.hn.hnms.biz.power.domain.vo.ChartDateVo;
import net.hn.hnms.biz.power.service.PowerService;
import net.hn.hnms.sys.common.core.domain.R;
import net.hn.hnms.sys.common.satoken.config.AutoSetMineCode;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotEmpty;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 智呈物联 pan'sx
 * 2022/9/20 17:41
 * 1.0 version
 */
@RestController
@RequestMapping("production/power")
@Api(value = "供电控制器", tags = {"供电接口"})
@RequiredArgsConstructor
public class PowerController {
    private final PowerService powerService;

    @GetMapping("currentData")
    @ApiOperation("供电实时数据")
    @AutoSetMineCode
    public R<List<DeviceInfo>> currentData(@ApiParam("系统编码") @NotEmpty(message = "系统编码不能为空") String systemCode, String mineCode) {
        return R.ok(powerService.currentData(systemCode, mineCode));
    }

    @GetMapping("powerSelect")
    @ApiOperation("供电所下拉框")
    @AutoSetMineCode
    public R<List<Map<String, Object>>> powerSelect(String mineCode) {
        return R.ok(powerService.powerSelect(mineCode));
    }

    @GetMapping("deviceInfo/pageList")
    @ApiOperation("供电设备列表")
    @AutoSetMineCode
    public R<ResponsePage<DeviceInfo>> devPage(@RequestParam(defaultValue = "0") int pageNum, @RequestParam(defaultValue = "20") int pageSize, DeviceInfo deviceInfo) {
        return R.ok(powerService.devPage(pageNum, pageSize, deviceInfo));
    }

    @GetMapping("sensorMonitor/pageList")
    @ApiOperation("实时列表")
    @AutoSetMineCode
    public R<ResponsePage<NowSensorMonitor>> sensormonitorPageList(@RequestParam(defaultValue = "0") int pageNum, @RequestParam(defaultValue = "20") int pageSize, NowSensorMonitor nowSensorMonitor) {
        return R.ok(powerService.sensormonitorPageList(pageNum, pageSize, nowSensorMonitor));
    }

    @GetMapping("hisSensorMonitor/pageList")
    @ApiOperation("历史列表")
    @AutoSetMineCode
    public R<ResponsePage<HisSensorMonitor>> hisSensormonitorPageList(@RequestParam(defaultValue = "0") int pageNum, @RequestParam(defaultValue = "20") int pageSize, HisSensorMonitor hisSensorMonitor) {
        return R.ok(powerService.hisSensormonitorPageList(pageNum, pageSize, hisSensorMonitor));
    }

    @ApiOperation("条件查询历史数据测点曲线")
    @GetMapping("pointHistoryCurve")
    @AutoSetMineCode
    public R<ChartDateVo> pointHistoryCurve(PointDto pointDto) {
        //开始时间  若为空默认为前6小时的日期 >=data_time
        if (pointDto.getStartTime() == null) {
            Calendar instance = Calendar.getInstance();
            instance.setTime(new Date());
            //日期减6小时
            instance.add(Calendar.HOUR_OF_DAY, -6);
            pointDto.setStartTime(instance.getTime());
        }
        if (pointDto.getEndTime() == null) {
            pointDto.setEndTime(new Date());
        }
        if (pointDto.getEndTime().getTime() - pointDto.getStartTime().getTime() > (3600 * 24 * 31 * 1000L)) {
            //1月得数据
            return R.fail("请选择1月内时间范围");
        }
        ChartDateVo chartDateVo = powerService.pointHistoryCurve(pointDto);
        return R.ok(chartDateVo);
    }

    @ApiOperation("条件查询历史数据测点列表")
    @GetMapping("pointHistoryList")
    @AutoSetMineCode
    public R<List<PointHistoryListVo>> pointHistoryList(PointDto pointDto) {
        //开始时间  若为空默认为前6小时的日期 >=data_time
        if (pointDto.getStartTime() == null) {
            Calendar instance = Calendar.getInstance();
            instance.setTime(new Date());
            //日期减6小时
            instance.add(Calendar.HOUR_OF_DAY, -6);
            pointDto.setStartTime(instance.getTime());
        }
        if (pointDto.getEndTime() == null) {
            pointDto.setEndTime(new Date());
        }
        if (pointDto.getEndTime().getTime() - pointDto.getStartTime().getTime() > (3600 * 24 * 31 * 1000L)) {
            //1月得数据
            return R.fail("请选择1月内时间范围");
        }
        List<PointHistoryListVo> list = powerService.pointHistoryList(pointDto);
        return R.ok(list);
    }

    @GetMapping("maxTime")
    @ApiOperation("数据最新时间")
    public R<List<DataStateVo>> getMaxTime(String mineCode, String companyCode) {
        return R.ok(powerService.getMaxTime(mineCode, companyCode));
    }

}
